package com.duqc.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duqc.util.ThreadPoolUtil;
import com.duqc.web.entity.MyBatisPlus;
import com.duqc.web.service.MyBatisPlusService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MyBatisPlusController
 * @Description
 * @Author duqc
 * @Date 2021/7/29
 **/
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mybatis/plus")
public class MyBatisPlusController {

    private final MyBatisPlusService myBatisPlusService;

    private static Map<String, String> map = new HashMap<>();

    @GetMapping("/list")
    public List<MyBatisPlus> list() {
        return this.myBatisPlusService.list();
    }

    @PostMapping("/insert")
    public Integer insert(@RequestBody MyBatisPlus myBatisPlus) {
        return this.myBatisPlusService.insert(myBatisPlus);
    }

    @PostMapping("/update")
    public Integer update(@RequestBody MyBatisPlus myBatisPlus) {
        return this.myBatisPlusService.update(myBatisPlus);
    }

    @GetMapping("/delete")
    public Integer delete(String id) {
        return this.myBatisPlusService.delete(id);
    }

    @GetMapping("/wrapper/query")
    public List<MyBatisPlus> wrapperQuery() {
        return this.myBatisPlusService.wrapperQuery();
    }

    @GetMapping(value = "/demo")
    public String demo() {
        System.out.println("打印测试");
        System.out.println("打印测试1");
        return "111111";
    }

    @GetMapping("/wrapper/update")
    public int wrapperUpdate() {
        return this.myBatisPlusService.wrapperUpdate();
    }

    @GetMapping("/wrapper/query/lambda")
    public List<MyBatisPlus> wrapperQueryLambda() {
        return this.myBatisPlusService.wrapperQueryLambda();
    }

    @GetMapping("/list/page")
    public IPage<MyBatisPlus> listPage() {
        return this.myBatisPlusService.listPage();
    }

    @GetMapping("oom")
    public String oom() {
        for (int i = 0; i < 100000; i++) {
            map.put("key" + i, "value" + i);
        }
        return "oom";
    }

    @GetMapping("params")
    public String getParams(HttpServletRequest request, HttpServletResponse response) {
        AsyncContext asyncContext = request.startAsync(request, response);
        String age = request.getParameter("age");
        System.out.println("age=" + age);
        ThreadPoolUtil.getPool().execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            String age1 = request.getParameter("age");
            System.out.println("age1=" + age1);
            asyncContext.complete();
        });
        return "success";
    }
}
